package com.cloudDemoUser.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloudDemoUser.service.UserService;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * user控制层
 *
 * @Author Daft_V
 * @Date 2022/5/5 23:32
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 查询所有用户列表
     *
     * @return JSONObject
     */
    @GetMapping("/userList")
    @ResponseBody
    public JSONObject userList() {
        List<User> userList = userService.userList();
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("data", userList);
        JSONObject jsonObject = new JSONObject(map);
        return jsonObject;
    }

    /**
     * 根据id查询用户
     *
     * @param id 主键
     * @return User
     */
    @GetMapping("queryUserById")
    @ResponseBody
    public User queryUserById(int id) {
        User user = userService.queryUserById(id);
        return user;
    }
}
